/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.KaColor;
import com.kaaction.modules.ka.service.KaColorService;

/**
 * 颜色Controller
 * @author huanghaiming
 * @version 2017-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/kaColor")
public class KaColorController extends BaseController {

	@Autowired
	private KaColorService kaColorService;
	
	@ModelAttribute
	public KaColor get(@RequestParam(required=false) String id) {
		KaColor entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kaColorService.get(id);
		}
		if (entity == null){
			entity = new KaColor();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:kaColor:view")
	@RequestMapping(value = {"list", ""})
	public String list(KaColor kaColor, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KaColor> page = kaColorService.findPage(new Page<KaColor>(request, response), kaColor); 
		model.addAttribute("page", page);
		return "modules/ka/kaColorList";
	}

	@RequiresPermissions("ka:kaColor:view")
	@RequestMapping(value = "form")
	public String form(KaColor kaColor, Model model) {
		model.addAttribute("kaColor", kaColor);
		return "modules/ka/kaColorForm";
	}

	@RequiresPermissions("ka:kaColor:edit")
	@RequestMapping(value = "save")
	public String save(KaColor kaColor, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kaColor)){
			return form(kaColor, model);
		}
		int cnt = kaColorService.getEqual(kaColor);
		if(cnt>0){
			model.addAttribute("message", "该颜色已存在");
			return form(kaColor, model);
		}
		kaColorService.save(kaColor);
		addMessage(redirectAttributes, "保存颜色成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaColor/?repage";
	}
	
	@RequiresPermissions("ka:kaColor:delete")
	@RequestMapping(value = "delete")
	public String delete(KaColor kaColor, RedirectAttributes redirectAttributes) {
		kaColorService.delete(kaColor);
		addMessage(redirectAttributes, "删除颜色成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaColor/?repage";
	}

}